<?php
/**
 * 接口自动重试
 *
 * @author kamisama.xia@gmail.com
 * @version 0.1
 */

class taskmgr_task_task_autoretryapi extends taskmgr_task_abstract {

    protected $_process_id = 'log_id';

    protected $_gctime = 1800;

    protected $_timeout = 120;

   /**
    * undocumented function
    *
    * @return void
    * @author 
    **/
   public function doTask($message, $queue = null)
   {
        if (empty($queue)) {
            $queue = $message;
        }

        $this->_exectime = microtime(true); 
        
        $body = $message->getBody();
        if($body){
            $content = json_decode($body, true);

            $now_time = time();
            if($now_time < $content['data']['exectime']){
                $wait_time = $content['data']['exectime'] - $now_time;
                sleep($wait_time);
            }

            $response = $this->curl($content);
            
            $this->writeLog($content, $response);

            $this->requeue($content, $response);
            
            //nack不起作用,信息请求处理完后判断结果以后，再判断是否要重新进队列
            $queue->ack($message->getDeliveryTag());
        }

        $gc = $this->isGC();
        if (!$gc) {
            if (method_exists($queue, 'cancel')) {
                $queue->cancel();
            }
        }
        return $gc;
   }
}